package com.cskaoyan.market.controller;

import com.cskaoyan.market.db.domain.MarketUser;
import com.cskaoyan.market.db.domain.MarketUserExample;
import com.cskaoyan.market.db.mapper.MarketUserMapper;
import com.cskaoyan.market.service.*;
import com.cskaoyan.market.util.JacksonUtil;
import com.cskaoyan.market.util.MybatisUtils;
import com.cskaoyan.market.util.ResponseUtil;
import com.cskaoyan.market.vo.FormVo;
import com.cskaoyan.market.vo.MarketGoodsVo;
import com.cskaoyan.market.vo.MarketOrderVo;
import com.cskaoyan.market.vo.MarketUserVo;
import org.apache.ibatis.session.SqlSession;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.time.LocalDate;
import java.util.*;

@WebServlet("/admin/stat/*")
public class AdminStatController extends HttpServlet {

    private MarketOrderService marketOrderService = new MarketOrderServiceImpl();
    private MarketGoodsService marketGoodsService = new MarketGoodsServiceImpl();

    AdminStatService adminStatService = new AdminStatServiceImpl();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String requestURI = req.getRequestURI();
        String op = requestURI.replace(req.getContextPath() + "/admin/stat/", "");
        if("user".equals(op)){
            user(req,resp);
        }            if ("order".equals(op)) {
            order(req, resp);
        }
        if ("goods".equals(op)) {
            goods(req, resp);
        }
    }

    private void user(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        SqlSession session = MybatisUtils.getSession();
        MarketUserMapper mapper = session.getMapper(MarketUserMapper.class);
        List<MarketUser> marketUsers = mapper.selectByExample(new MarketUserExample());
        HashMap<LocalDate, Integer> userNumGroupByLastLoginTime = new HashMap<>();
        //先查表拿数据，处理数据得到一个HashMap
        for (MarketUser marketUser : marketUsers) {
            LocalDate lastLoginTime = marketUser.getLastLoginTime().toLocalDate();
            Integer userNum = userNumGroupByLastLoginTime.getOrDefault(lastLoginTime, 0);
            userNumGroupByLastLoginTime.put(lastLoginTime,userNum + 1);
        }
        //把数据封装到MarketUserVo中
        MarketUserVo marketUserVo = new MarketUserVo();
        marketUserVo.setColumns(Arrays.asList("day","users"));
        List<MarketUserVo.UserNumByDay> userNumByDayList = new ArrayList<>();
        for (Map.Entry<LocalDate, Integer> entry : userNumGroupByLastLoginTime.entrySet()) {
            MarketUserVo.UserNumByDay userNumByDay = new MarketUserVo.UserNumByDay(entry.getKey(), entry.getValue());
            userNumByDayList.add(userNumByDay);
        }
        marketUserVo.setRows(userNumByDayList);
        //响应出去
        resp.getWriter().println(JacksonUtil.getObjectMapper().writeValueAsString(ResponseUtil.ok(marketUserVo)));
    }

    private void goods(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        MarketGoodsVo marketGoodsVo = marketGoodsService.getGoods();
        Object ok = ResponseUtil.ok(marketGoodsVo);
        resp.getWriter().println(JacksonUtil.getObjectMapper().writeValueAsString(ok));
    }

    private void order(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        MarketOrderVo marketOrderVo = marketOrderService.getOrder();
        Object ok = ResponseUtil.ok(marketOrderVo);
        resp.getWriter().println(JacksonUtil.getObjectMapper().writeValueAsString(ok));
    }
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        super.doPost(req, resp);
    }



}
